<?php

require_once 'Core/DAO.php';

class AccountDAO extends Core_DAO
{
	protected $_name        = 'account';
	protected $_primary 	= 'account_id';
	protected $_viewAccount = 'view_accounts';
	
	const SUSPEND       = 0;
	const ACTIVATE      = 0;     
	
	/**
	 * This Method will handel the Sign up/Create New Account functionallity.
	 * 
	 * @param array $accountData
	 * @return int
	 */
	public function createAccount(array $accountData)
	{
		return $this->insert($accountData);
	}
	
	/**
	 * 
	 * @param unknown_type $email
	 */
	public function isAccountExist($email)
	{
		$result = $this->select()
					   ->from($this->_name)
					   ->where('email=?', $email)
					   ->query()
					   ->fetchColumn();
		return $result;	
	}
	/**
	 * This Method is used to update the Account details.
	 * 
	 * @param int   $accountId
	 * @param array $accountArray
	 * @return int
	 */
	public function updateAccount($accountId, $accountArray)
	{
		$where = array('account_id' => $accountId);
		return $this->updateData($accountArray, $where); 	
	}
	
	/**
	 * Used to get the Account Details of the User.
	 * 
	 * @param int $accountId
	 * @return array
	 */
	public function getAccount($accountId)
	{
		$result = $this->getDefaultAdapter()
						->select()
					    ->from($this->_viewAccount)
					    ->where('account_id=?', $accountId)
					    ->query()
					    ->fetchAll();
						   
		return $result;					   
	}
	
	/**
	 * Used to get the Account Details of the User.
	 * 
	 * @param string $email
	 */
	public function getAccountByEmail($email)
	{
		$result = $this->getDefaultAdapter()
					   ->select()
					   ->from($this->_viewAccount)
					   ->where('email=?', $email)
					   ->query()
					   ->fetchAll();
		return $result;					   
	}
	
	/**
	 * Used to Suspend a account
	 * 
	 * @param int $accountId
	 * @return int
	 */
	public function suspendAccount($accountId)
	{
		$where  = $this->_primary.'='.$accountId;
		$data   = array('status' => self::SUSPEND);
		$result = $this->updateData($data, $where);	
		
		return $result;
	}
	
	/**
	 * Used to Activated a suspended account
	 * 
	 * @param int userId
	 * @return int
	 */
	public function activateAccount($accountId) 
	{
		$where  = $this->_primary.'='.$accountId;
		$data   = array('status' => self::ACTIVATE);
		$result = $this->updateData($data, $where);	
		
		return $result;
	}
}